import 'package:flutter/material.dart';
import 'package:library_base/base/base_list_state.dart';
import 'package:library_base/constant/constant.dart';
import 'package:library_base/entities/event/scheduled_filter_event.dart';
import 'package:library_base/entities/event/scheduled_refresh_event.dart';
import 'package:library_base/entities/schedul_child_entity.dart';
import 'package:library_base/entities/schedul_entity.dart';
import 'package:library_base/event/event.dart';
import 'package:library_base/ext/list_ext.dart';
import 'package:library_base/ext/theme_data_ext.dart';
import 'package:library_base/global/multi_language.dart';
import 'package:get/get.dart';
import 'package:library_base/res/gaps.dart';
import 'package:library_base/res/styles.dart';
import 'package:library_base/utils/theme_util.dart';
import 'package:library_base/utils/toast_util.dart';
import 'package:library_base/widget/button/gradient_button.dart';
import 'package:library_base/widget/image/local_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:library_base/widget/multi_state_widget/multi_state_widget.dart';
import 'package:module_dispatcher/page/scheduled/scheduled_controller.dart';
import 'package:module_dispatcher/page/scheduled/scheduled_page_ui.dart';

class ScheduledPage extends StatefulWidget {
  const ScheduledPage({Key? key}) : super(key: key);

  @override
  _ScheduledPageState createState() => _ScheduledPageState();
}

class _ScheduledPageState
    extends BaseListState<SchedulEntity, SchedulChildEntity, ScheduledPage, ScheduledController> {
  @override
  void onCreate() {
    setNavigationBarVisible(false);
    setStatusBarVisible(false);
    Event.eventBus.on<ScheduledRefreshEvent>().listen((event) {
      baseGetxController!.reset();
    });
    Event.eventBus.on<ScheduledFilterEvent>().listen((event) {
      baseGetxController!.stationTimeBegin = event.stationTimeBegin;
      baseGetxController!.tryReLoad();
    });
  }

  @override
  void controllerOnCreate() {
    super.controllerOnCreate();
    baseGetxController!.callback = refresh;
  }

  void refresh() {
    baseGetxController!.tryReLoad();
  }

  @override
  ScheduledController registerController() => Get.put(ScheduledController());

  @override
  bool get wantKeepAlive => true;

  @override
  Widget? getMainWidget(BuildContext context) {
    return MultiStateWidget(
      emptyWidget: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            LocalImage('common_empty', package: Constant.packLib(), width: 0.6.sw),
            Text(MultiLanguage.noSuchProduct.tr, style: TextStyles.textSecondary28),
            SizedBox(height: 80.h),
            Row(mainAxisAlignment: MainAxisAlignment.center, children: [
              GradientButton(
                ThemeUtil.getTheme(),
                width: 0.4.sw,
                height: 80.w,
                text: MultiLanguage.backToList.tr,
                onPressed: baseGetxController!.backToList,
              ),
              SizedBox(width: 40.w),
              GradientButton(
                ThemeUtil.getTheme(),
                width: 0.4.sw,
                height: 80.w,
                text: MultiLanguage.reload.tr,
                onPressed: baseGetxController!.tryReLoad,
              ),
            ])
          ]),
      controller: baseGetxController!.multiControllerOut,
      buildChild: (){
        return buildBottom(
          baseGetxController: baseGetxController!,
          child: Container(
              color: ThemeUtil.getTheme().secondaryBgColor(),
              child: getListView(context, baseGetxController)),
          platoon: baseGetxController!.platoon,
        );
      },
    );
  }

  @override
  List<Widget> buildListItem(BuildContext context) {
    return baseGetxController!.listData.value.buildItem((index, element) {
      return buildItem(
        element,
        onTap: baseGetxController!.onItemChildClick,
        onItemClick: baseGetxController!.onItemClick,
      );
    }, dividingLine: () {
      return Gaps.vwGap18;
    });
  }

  ///显示选中的商品列表
  void onShowSelectedGoods() {
    if (baseGetxController!.listDataSelected.isEmpty) {
      ToastUtil.normal(MultiLanguage.pleaseSelectTheItemBeforeProceeding.tr);
      return;
    }
  }
}
